import utils from "@common/utils/utils";

const rewriteChangeRow = ({ row, column, $table }, insertProp, callback) => {
	const columnData = $.extend({}, column.params);
	if (columnData.prop.indexOf('preset_qty_') !== -1) {
		let columns = $table.tableFullColumn.filter(item => item.params && utils.isNotEmpty(item.params.prop));
		columns = columns.filter(item => item.params.prop.indexOf('preset_qty_') !== -1);
		let columnsFileld = columns.map(item => item.params.prop);
		let totalQty = 0;
		columnsFileld.forEach(field => {
			if (utils.isNotEmpty(row[field])) {
				totalQty = utils.accAdd(totalQty, row[field]);
			}
		});
		$table.$set(row, insertProp, totalQty);
		typeof callback === 'function' && callback(row);
		if (utils.isNotEmpty(row.price)) {
			let amount = utils.accMul(row[insertProp], row.price);
			$table.$set(row, 'amount', amount);
			if (utils.isNotEmpty(row.tax_rate)) {
				let addRate = utils.accAdd(1, row.tax_rate);
				let taxAmount = utils.accDiv2Fixed(row.amount, addRate, 4);
				taxAmount = utils.accMul2Fixed(taxAmount, row.tax_rate, 2);
				$table.$set(row, "tax_amount", taxAmount);
			}
		}
	}
};

export default {
	isClothing(setting) {
		return setting && setting.industry_config === 'clothing';
	},
	getSizeList(setting) {
		if (setting && this.isClothing(setting) && utils.isNotEmpty(setting.clothing_preset_size)) {
			return setting.clothing_preset_size.replace(/，/g, ',').replace(/；/g, ',').replace(/;/g, ',').split(',');
		}
		return [];
	},
	getPresetQtyField(columns) {
		let filter = columns.filter(item => utils.isNotEmpty(item.prop) && item.prop.indexOf('preset_qty_') !== -1);
		return filter.map(item => item.prop);
	},
	getPresetProduct(presetProductMap) {
		return Object.keys(presetProductMap || {}).map(key => presetProductMap[key]);
	},
	resetTableColumns(setting, columns, insertProp = 'qty', checkFun, callback) {
		let sizeList = this.getSizeList(setting);
		if (utils.isNotEmptyList(sizeList)) {
			let sizeColumns = sizeList.map((item, index) => {
				return {
					label: item,
					prop: `preset_qty_${ index }`,
					align: "center",
					width: 70,
					editRender: {
						name: "CustomInput",
						autoselect: true,
						props: {
							integer: true,
						},
						events: {
							input: e => rewriteChangeRow(e, insertProp, callback),
						},
						filter(row, column) {
							if (utils.isNotEmpty(row.product_sid) && utils.isNotEmpty(row.spec_preset_value)) {
								let check = `,${row.spec_preset_value},`.indexOf(`,${item},`) !== -1;
								if (check) {
									if (typeof checkFun === 'function') {
										return checkFun(row, column, `preset_qty_${ index }`);
									}
									return true;
								}
							}
							return false;
						},
					},
					formatter(row) {
						if (utils.isNotEmpty(row.product_sid) && utils.isNotEmpty(row.spec_preset_value)) {
							let check = `,${row.spec_preset_value},`.indexOf(`,${item},`) !== -1;
							if (check) {
								return row[`preset_qty_${ index }`] || '';
							}
							return `<span style="color: red;">无此尺寸</span>`;
						}
						return '';
					},
				};
			});
			let specIndex = columns.findIndex(item => item.prop === insertProp);
			let newColumns = [];
			columns.forEach((item, index) => {
				let obj = $.extend({}, item);
				if (['spec_name', 'goods_spec', 'spec_info', 'goods_specifications'].indexOf(item.prop) !== -1) {
					obj.label = '颜色';
					obj.prop = 'clothing_color_name';
				}
				if (index === specIndex) {
					newColumns = newColumns.concat(sizeColumns);
					obj.label = '小计';
					delete obj.editRender;
					newColumns.push(obj);
				} else {
					newColumns.push(obj);
				}
			});
			return newColumns;
		}
		return columns;
	},
	genInitData(setting, data) {
		let sizeList = this.getSizeList(setting);
		let initData = {};
		if (utils.isNotEmptyList(sizeList)) {
			sizeList.forEach((size, index) => {
				initData[`preset_qty_${ index }`] = null;
			});
		}
		if (data) {
			initData = $.extend({}, data, initData);
		}
		return initData;
	},
	procSubData(setting, data) {
		let list = [];
		if (utils.isNotEmptyList(data)) {
			let sizeList = this.getSizeList(setting);
			data.forEach(item => {
				let obj = $.extend({}, item);
				if (utils.isNotEmptyList(sizeList)) {
					let sizeMap = {};
					sizeList.forEach((size, index) => {
						if (utils.isNotEmpty(item[`preset_qty_${ index }`])) {
							sizeMap[size] = item[`preset_qty_${ index }`];
						}
						delete obj[`preset_qty_${ index }`];
					});
					obj.preset_qty_map = sizeMap;
					delete obj.qty;
					delete obj.amount;
					delete obj.in_qty;
					delete obj.out_qty;
				}
				list.push(obj);
			});
		}
		return list;
	},
	transferDetailList(setting, data, columns = []) {
		const _this = this;
		let list = [];
		if (utils.isNotEmptyList(data)) {
			let sizeList = _this.getSizeList(setting);
			data.forEach(item => {
				let obj = $.extend({}, item);
				sizeList.forEach((size, index) => {
					obj[`preset_qty_${ index }`] = null;
				});
				if (_this.isClothing(setting) && item.preset_qty_map) {
					for (let key in item.preset_qty_map) {
						obj[key] = item.preset_qty_map[key];
					}
				}
				columns.forEach(col => {
					if (utils.isEmpty(obj[col.prop])) {
						obj[col.prop] = null;
					}
				});
				list.push(obj);
			});
		}
		return list;
	},
};
